<?php

function insertLabel($name){
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if ($dbLink) {
		$name = mysql_real_escape_string($name);
		$result_query = mysql_query("select * from label where name like '$name'");
		if (mysql_num_rows($result_query) == 0) {
			$result = mysql_query("insert into label (name)
				values ('$name')",$dbLink);
			if (!$result) {
				redirectError("label not inserted");
			}
			else {
				$labelId = mysql_insert_id($dbLink);
				transactionClose();
				return $labelId;
			}
		}
		else {
			transactionClose();
			redirectError("label already in data base");
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}
} //end insertLabel


function findLabelByLabelId($labelId){
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if (($labelId > 0) && ($dbLink)){
		$result_query = mysql_query("select * from label where labelId = $labelId");
		transactionClose();
		if (mysql_num_rows($result_query) == 1) {
			$label = mysql_fetch_assoc($result_query);
			return $label;
		}
		else{
			redirectError("problems to get label information");
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}
	

}// end findLabelByLabelId



function findLabelByName($name){
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if ($dbLink) {
		$name = mysql_real_escape_string($name);
		$result_query = mysql_query("select * from label where name like '$name'");
		transactionClose();
		if (mysql_num_rows($result_query) == 1) {
			$label = mysql_fetch_assoc($result_query);
			return $label;
		}

	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}
} //end findLabelByName


function findLabels() {
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if ($dbLink) {
		$result = mysql_query("SELECT * FROM label order by name");
		transactionClose();
		if (mysql_num_rows($result) > 0) {
			$count = mysql_num_rows($result);
			for ($i=0;$i<$count;$i++) {
				$result2[$i]= mysql_fetch_assoc($result);
			}
			return $result2;
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	} //end findLabels
}

?>